package gao.xiaolei.dao;

import gao.xiaolei.entity.Classes;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository("ClassesDao")
public interface ClassesDao extends JpaRepository<Classes, String> {

    @Query("select id,name from Classes where leader=:leader")
    public List<Object[]> findByLeader(@Param("leader") String leader);

    @Query("select id,name from Classes where creater=:creater")
    public List<Object[]> findByCreater(@Param("creater") String creater);

    @Query("select leader from Classes where id=:id")
    public String getLeader(@Param("id") String classes);

    @Query("select name from Classes where id=:id")
    public String getName(@Param("id") String classes);

    @Query("select creater from Classes where id=:id")
    public String getCreater(@Param("id") String classes);

    @Query("select max(stage) from Classes")
    public Optional<Integer> getMaxStage();

    public Integer countAllByLeader(String leader);

    public Integer countAllByCreater(String creater);

    @Modifying
    @Query("update Classes set creater=:creater where id=:id")
    public void updateCreater(@Param("creater") String creater,
                              @Param("id") String id);

    @Modifying
    @Query("update Classes set isAutomatic=:isAutomatic where id=:id")
    public void updateIsAutomatic(@Param("isAutomatic") boolean isAutomatic,
                              @Param("id") String id);
}
